package com.cg.intertview;

import org.junit.Test;

import java.util.Arrays;

/**
 * 程序员面试金典 16.01.交换数字
 *
 * @program: LeetCode->Interview_16_01
 * @author: cg
 * @create: 2022-04-10 21:33
 **/
public class Interview_16_01 {

    @Test
    public void test16_01() {
        System.out.println(Arrays.toString(swapNumbers(new int[]{1, 2})));
    }

    /**
     * 编写一个函数，不用临时变量，直接交换numbers = [a, b]中a与b的值。
     * <p>
     * 示例：
     * 输入: numbers = [1,2]
     * 输出: [2,1]
     * <p>
     * 提示：
     * numbers.length == 2
     * -2147483647 <= numbers[i] <= 2147483647
     *
     * @param numbers
     * @return
     */
    public int[] swapNumbers(int[] numbers) {
        numbers[0] = numbers[0] ^ numbers[1];
        numbers[1] = numbers[0] ^ numbers[1];
        numbers[0] = numbers[0] ^ numbers[1];
        return numbers;
    }

}
